using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace EARogueCalc
{
    public partial class ItemLookup : Form
    {
        public ItemLookup()
        {
            InitializeComponent();
            if (!File.Exists(Statics.ITEM_LIST_PATH))
            {
                GetAllakhazamItemList();
            }
        }

        private void ToggleUIEnable(bool enabled)
        {
            itemName.Enabled = enabled;
            searchButton.Enabled = enabled;
            loadLink.Enabled = enabled;
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            string itemId = Statics.GetItemId(itemName.Text.Trim());
            if (itemId == Statics.ITEM_DEFAULT_NONITEM)
                MessageBox.Show(
                    "Item not found.  Please check spelling and case and try again, or refresh the item database");
            else
                MessageBox.Show(itemId);
        }

        private void GetAllakhazamItemList()
        {
            MessageBox.Show("Please wait while we initialize the item list from Allakhazam");
            ToggleUIEnable(false);
            XmlDocument itemList = Statics.GetFeed(Statics.ITEM_LIST_XML_URI);
            itemList.Save(Statics.ITEM_LIST_PATH);
            ToggleUIEnable(true);
            
        }

        private void loadLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            GetAllakhazamItemList();
        }
    }
}